layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaInterval = new function () {
            // 初始化
            this.init = function (intervalName, resData, callBack, time) {
                // 判斷時間
                if(time == undefined || time == null || time == "") { time = 1000; }
                // 開始
                top.intervalData[intervalName]["time"] = new Date().getTime();
                top.intervalData[intervalName]["value"] = setInterval(() => {
                    typeof callBack == "function" && callBack(resData)
                }, time);
            }
            // 获取值
            this.getValue = function (intervalName) {
                var value = top.intervalData[intervalName]["value"];
                if (value != undefined && value != null) {
                    if(value != ""){
                        return value;
                    } else {
                        return "true"
                    }
                } else {
                    return "";
                }
            }
            // 获取时间
            this.getTime = function () {
                var value = top.intervalData[intervalName]["time"];
                if (value != undefined && value != null) {
                    if(value != ""){
                        return value;
                    } else {
                        return "true"
                    }
                } else {
                    return "";
                }
            }
            // 判断
            this.judge = function (judgeStartTime, time, resData, callBack1, callBack2) {
                 // 當前時間
                var currTime = new Date().getTime();
                var syTime = (currTime - parseInt(time)) / 1000;
                var syxx = judgeStartTime - syTime;
                if (syxx <= 0) {
                   typeof callBack1 == "function" && callBack1(resData, syxx)
                } else {
                   typeof callBack2 == "function" && callBack2(resData, syxx)
                }
            }
            // 清除
            this.clear = function (intervalName, resData, callBack) {
                if(resData != undefined && resData.sessionStorageValue != undefined){
                    sessionStorage.removeItem(resData.sessionStorageValue);
                }
                if(resData != undefined && resData.sessionStorageTime != undefined){
                    sessionStorage.removeItem(resData.sessionStorageTime);
                }
                typeof callBack == "function" && callBack(resData)
                clearInterval(this.getValue(intervalName));
            }
        };
        exports('kaInterval', kaInterval);
    }
)